package com.atguigu.redislock.test;

import java.awt.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @auther zzyy
 * @create 2022-10-30 10:31
 */
public class ReEnterDemo
{
    static Object myLock = new Object();
    private static Lock lock = new ReentrantLock();

    public static void main(String[] args)
    {
        new Thread(() -> {
            lock.lock();
            try
            {
                System.out.println(Thread.currentThread().getName()+"\t"+"lock 111");
                lock.lock();
                try
                {
                    System.out.println(Thread.currentThread().getName()+"\t"+"lock 222");
                }finally {
                    lock.unlock();
                }
            }finally {
                lock.unlock();
            }
        },"t1").start();
        //暂停毫秒
        try { TimeUnit.MILLISECONDS.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); }
        new Thread(() -> {
            lock.lock();
            try
            {
                System.out.println(Thread.currentThread().getName()+"\t"+"lock 333");
            }finally {
                lock.unlock();
            }
        },"t2").start();


        System.out.println("----main ^_^");
    }

    private static void m1()
    {
        new Thread(() -> {
            synchronized (myLock)
            {
                System.out.println(Thread.currentThread().getName()+"\t"+"---come in 111");
                synchronized (myLock)
                {
                    System.out.println(Thread.currentThread().getName()+"\t"+"---come in 222");
                    synchronized (myLock)
                    {
                        System.out.println(Thread.currentThread().getName()+"\t"+"---come in 333");
                    }
                }
            }
        },"t1").start();
    }
}
